<?php
declare (strict_types=1);

namespace quick\admin\components;


use quick\admin\components\element\ElTreeV2;
use quick\admin\Element;

class QuickTree extends ElTreeV2
{

    public $component = "qk-tree";


    /**
     * QuickTree constructor.
     * @param array $data
     * @param string $title
     */
    public function __construct(array $data = [],string $title = '')
    {
        $this->props('data',$data);
        $this->props('title',$title);

    }



    /**
     * 右边动作
     * @var array
     */
    private $actions = [];

    /**
     * @param int $height
     * @return QuickTree
     */
    public function height(int $height)
    {
        return $this->props(__FUNCTION__, $height );
    }


    /**
     * 显示全部展开按钮
     *
     * @return QuickTree
     */
    public function showExpandAll()
    {
        return $this->props('showExpandAll', true);
    }


    /**
     * 默认全部展开
     * @return QuickTree
     */
    public function defaultAllOpen()
    {
        return $this->props('defaultAllOpen', true);
    }

    /**
     * 显示全选按钮
     *
     * @return QuickTree
     */
    public function showCheckAll()
    {
        return $this->props('showCheckAll', true);
    }


    /**
     * 设置边框
     * @param string $color
     * @return QuickTree
     */
    public function border(string $color = ' #EBEEF5')
    {
        return $this->style('border',"1px solid {$color}");
    }


    /**
     * 搜索
     *
     * @param string $searchPlaceholder
     * @return QuickTree
     */
    public function search(string $searchPlaceholder = '搜索')
    {
        $this->props('searchPlaceholder', $searchPlaceholder);
        return $this->props('search', true);
    }


    /**
     *
     * @param Element $action
     * @param int $level 限制在$level内显示 0不限制
     * @return $this
     */
    public function addAction(Element $action, int $level = 0)
    {
        $action->withMeta(['level' => $level]);
        $this->actions = array_merge($this->actions, [$action]);
        return $this;

    }

    /**
     * @param Element $element
     * @return QuickTree
     */
    public function treeItem(Element $element)
    {
        return $this->props('display', $element);
    }


    /**
     * @param Element $element
     * @return QuickTree
     */
    public function headerAction(Element $element)
    {
        return $this->props('headerAction', $element);
    }


    /**
     * 可选
     *
     * @return QuickTree
     */
    public function showCheckbox()
    {
        return $this->props('showCheckbox', true);
    }


    /**
     * @return $this
     */
    public function isFilter()
    {
        $this->props('isFilter', true);
        return $this;
    }


    /**
     * @param string $value
     * @return $this
     */
    public function tabKey(string $value)
    {
        $this->props('tabKey', $value);
        return $this;
    }


    /**
     * @param string $url
     * @return QuickTree
     */
    public function lazy(string $url)
    {
        return $this->props('lazy',$url);
    }


    public function getChildrenComponents(): array
    {
        return array_merge(parent::getChildrenComponents(),$this->actions);
    }

    public function jsonSerialize(): array
    {
        if (!empty($this->actions)) {
            $this->treeItem(Component::custom('qk-tree-item')
                ->props('actions', $this->actions));
        }
        return parent::jsonSerialize(); // TODO: Change the autogenerated stub
    }

}
